package com.pts_socuse.service.impl;

import com.pts_socuse.dao.DiseaseRecordMapper;
import com.pts_socuse.dao.IssueRecordMapper;
import com.pts_socuse.dao.ManagerAnimalMapper;
import com.pts_socuse.dao.ManagerFenceHouseMapper;
import com.pts_socuse.service.AnalysisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnalysisServiceImpl implements AnalysisService {

    @Autowired
    private ManagerFenceHouseMapper managerFenceHouseMapper;
    @Autowired
    private DiseaseRecordMapper diseaseRecordMapper;
    @Autowired
    private ManagerAnimalMapper managerAnimalMapper;
    @Autowired
    private IssueRecordMapper issueRecordMapper;

    @Override
    public Map<String, List<Object>> countNum() {
        // 名字的集合
        List<Object> list = List.of("栏舍","栏圈","动物","冷库","员工");
        // 数据集合
        List<Object> integers = managerFenceHouseMapper.countAllResources();
        // 定义map集合
        Map<String, List<Object>> map = new HashMap<>();
        map.put("name", list);
        map.put("value", integers);
        return map;
    }

    @Override
    public Map<String, Long> countDisease() {
        List<Map<String, Object>> maps = diseaseRecordMapper.countDisease();
        Map<String, Long> map = new HashMap<>();
        maps.forEach(s->{
            map.put((String)s.get("d_name"), Long.parseLong(s.get("count")+""));
        });
        return map;
    }

    @Override
    public Map<String, Integer> countWeight() {

        // 名字
        List<Object> list = List.of("30以下","30~50","50以上");
        // 数据
        List<Integer> stringIntegerMap = managerAnimalMapper.countWeight();
        // 定义map集合
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            map.put(list.get(i)+"", stringIntegerMap.get(i));
        }
        return map;
    }

    @Override
    public Map<String, List<Object>> countSales() {
        // 名字，定义从1月到12月的月份字符串列表
        List<Object> list = List.of("1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月");
        // 数据列表，里面的数据参照list，全部定义为0
        Integer[] array = new Integer[12];
        Arrays.fill(array, 0);
        List<Object> integers = Arrays.asList(array);
        // 查询
        List<Map<String, Integer>> baseData = issueRecordMapper.countSales();
        for (Map<String, Integer> map : baseData) {
            int index = map.get("月份") - 1;
            int count = new BigDecimal(map.get("销量")+"").intValue();
            integers.set(index, count);
        }
        HashMap<String, List<Object>> map = new HashMap<>();
        map.put("name",list);
        map.put("value",integers);
        return map;
    }
}